package com.ruoyi.web.controller.monitor

import com.ruoyi.common.annotation.Log
import com.ruoyi.common.core.controller.BaseController
import com.ruoyi.common.core.domain.AjaxResult
import com.ruoyi.common.core.page.TableDataInfo
import com.ruoyi.common.enums.BusinessType
import com.ruoyi.common.utils.poi.ExcelUtil
import com.ruoyi.framework.web.service.SysPasswordService
import com.ruoyi.system.domain.SysLogininfor
import com.ruoyi.system.service.ISysLogininforService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpServletResponse

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/logininfor")
open class SysLogininforController : BaseController() {
    @Autowired
    private lateinit var  logininforService: ISysLogininforService
    @Autowired
    private lateinit var  passwordService: SysPasswordService

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    open fun list(logininfor: SysLogininfor?): TableDataInfo {
        startPage()
        val list = logininforService.selectLogininforList(logininfor)
        return getDataTable(list)
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    open fun export(response: HttpServletResponse, logininfor: SysLogininfor) {
        val list = logininforService.selectLogininforList(logininfor)
        val util: ExcelUtil<SysLogininfor> = ExcelUtil(SysLogininfor::class.java)
        util.exportExcel(response, list, "登录日志")
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    open fun remove(@PathVariable infoIds: Array<Long>): AjaxResult {
        return toAjax(logininforService.deleteLogininforByIds(infoIds))
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    open fun clean(): AjaxResult {
        logininforService.cleanLogininfor()
        return success()
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    open fun unlock(@PathVariable("userName") userName: String?): AjaxResult {
        passwordService.clearLoginRecordCache(userName)
        return success()
    }
}
